package com.voicechat.firstmet.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseActivity;
import com.voicechat.firstmet.bean.MySelfInfoBean;
import com.voicechat.firstmet.bean.PersonDataBean;
import com.voicechat.firstmet.net.HTTPAPI;
import com.voicechat.firstmet.net.service.LoginService;
import com.voicechat.firstmet.net.service.MeInfoService;
import com.voicechat.firstmet.utils.DataCleanManager;
import com.voicechat.firstmet.utils.ParcelableUtils;
import com.voicechat.firstmet.utils.PhoneUtils;
import com.voicechat.firstmet.utils.SPUtils;
import com.voicechat.firstmet.utils.StatusBarUtils;
import com.voicechat.firstmet.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.activity_imessage
 * @ClassName: AccountSettingActivity
 * @Description: 账户设置页面
 * @Author: ljp
 * @CreateDate: 2018/9/27 14:06
 * @Version: 1.0
 */
public class AccountSettingActivity extends BaseActivity {
    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.linear_account_safely)
    LinearLayout linearAccountSafely;
    @BindView(R.id.tv_cache)
    TextView tvCache;
    @BindView(R.id.linear_clear_cache)
    LinearLayout linearClearCache;
    @BindView(R.id.linear_about_yy)
    LinearLayout linearAboutYy;
    @BindView(R.id.linear_exit)
    LinearLayout linearExit;
    @BindView(R.id.linear_service)
    LinearLayout linearService;
    private String TAG = "AccountSettingActivity";
    private MySelfInfoBean.ObjectBean mySeleInfo;
    private String userId;
    private String token;
    private String deviceInfo;

    @Override
    protected int setStatusBarColor() {
        return 0;
    }
    @Override
    protected boolean isSettingStatusBarText() {
        return false;
    }
    @Override
    protected int loadLayout() {
        return R.layout.activity_account_setting;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        //透明状态栏
        StatusBarUtils.Full(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        mySeleInfo = MySelfInfoBean.ObjectBean.CREATOR.createFromParcel(ParcelableUtils.unmarshall(
                SPUtils.getEditoString(SPUtils.SP_MYSELFINFO)));
        userId = mySeleInfo.getId();
        token = SPUtils.getEditoString(SPUtils.SP_TOKEN);
        deviceInfo = PhoneUtils.getDeviceInfo(context);
        initView();

    }

    private void initView() {
        try {
            String totalCacheSize = DataCleanManager.getTotalCacheSize(context);
            tvCache.setText(totalCacheSize);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @OnClick({R.id.linear_back, R.id.linear_account_safely, R.id.linear_clear_cache,
            R.id.linear_about_yy, R.id.linear_exit,R.id.linear_service,R.id.linear_real_name,R.id.linear_wo_look_me})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back://返回
                finish();
                break;
            case R.id.linear_account_safely://账户安全
                startActivity(new Intent(activity,AccountSafelyActivity.class));
                break;
            case R.id.linear_clear_cache://清除缓存
                showCacheDialog();
                break;
            case R.id.linear_about_yy://关于隐隐
                startActivity(new Intent(activity,AboutYYActivity.class));
                break;
            case R.id.linear_exit://退出登录
                showNormalDialog();
                break;
            case R.id.linear_service://意见与反馈
                startActivity(new Intent(activity, ServiceAndHelpActivity.class));
                break;
            case R.id.linear_real_name://实名认证
                judeRealName();
                break;
            case R.id.linear_wo_look_me://谁看过我
                Intent intent = new Intent(activity, LookMeActivity.class);
                intent.putExtra("vType", mySeleInfo.getvType());
                startActivity(intent);
                break;
        }
    }
    /**
     * @return
     * @method
     * @description 实名认证状态判断
     * @date: 2018/11/6 15:07
     * @author: ljp
     */
    private void judeRealName() {
        Intent intent = null;
        //判断是否提交过实名认证信息
        if (mySeleInfo.getIsAttestation() == 0) {// 实名认证任务 0未认证 1 已审核 2 审核中 3驳回
            intent = new Intent(activity, RealNameActivity.class);
            startActivity(intent);
        } else {//上传过就去判断，实名认证的当前的状态
            MeInfoService.certificationSelect(mySeleInfo.getId(), token, PhoneUtils.getDeviceInfo(context)).subscribe(personDataBean -> {
                if (personDataBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                    //判断state状态  认证状态 0审核中 1审核通过 2驳回
                    PersonDataBean.ObjectBean object = personDataBean.getObject();
                    if (object.getStatus() == 0) {
                        toast_MakeText(getResources().getString(R.string.real_name));
                    } else if (object.getStatus() == 1) {
                        Intent intent2 = new Intent(activity, RealNameActivity.class);
                        intent2.putExtra("object", object);
                        startActivity(intent2);
                    } else if (object.getStatus() == 2) {
                        toast_MakeText(getResources().getString(R.string.real_name_no));
                        Intent intent3 = new Intent(activity, RealNameActivity.class);
                        startActivity(intent3);
                    }
                } else if (personDataBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                    Utils.setToken(context);
                } else {
                    toast_MakeText(personDataBean.getMsg());
                }
            }, throwable -> {
                Log.i(TAG, "judeRealName: ---------" + throwable.toString());

            });

        }
    }
    /**
     * @method
     * @description 缓存清理
     * @date: 2018/11/1 19:37
     * @author: ljp
     * @return
     */
    private void showCacheDialog(){
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(context);
        //normalDialog.setTitle("我是一个普通Dialog");
        normalDialog.setMessage("确认清除所有缓存?");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        DataCleanManager.clearAllCache(context);
                        dialog.dismiss();
                        try {
                            String totalCacheSize = DataCleanManager.getTotalCacheSize(context);
                            tvCache.setText(totalCacheSize);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        dialog.dismiss();
                    }
                });
        // 显示
        normalDialog.show();
    }
    /**
    * @method 
    * @description 退出登录
    * @date: 2018/11/1 19:37
    * @author: ljp
    * @return 
    */
    private void showNormalDialog(){
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(context);
        //normalDialog.setTitle("我是一个普通Dialog");
        normalDialog.setMessage("是否确认退出?");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        initLoginOut(dialog);
                    }
                });
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        dialog.dismiss();
                    }
                });
        // 显示
        normalDialog.show();
    }
    /**
    * @method
    * @description 退出登录
    * @date: 2018/11/1 19:28
    * @author: ljp
    * @return
    */
    private void initLoginOut(DialogInterface dialog) {
        LoginService.loginExit(userId,token,deviceInfo).subscribe(result -> {
            Log.i(TAG, "initLoginOut: ------"+result.toString());
            /*if (result.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS){

            }else if(result.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN){
                dialog.dismiss();
                DataCleanManager.clearAllCache(context);//清楚缓存
                WsManager.getInstance(context).disconnect();//断开长链接
                context.startActivity(new Intent(context, LoginActivity.class));
                App.getInstance().removeAllActivity_();
            }else {
                dialog.dismiss();
                Utils.setToken(context);
                //toast_MakeText(getResources().getString(R.string.net_error));
            }*/
        },throwable -> {
          /*  dialog.dismiss();
            Utils.setToken(context);*/
           // toast_MakeText(getResources().getString(R.string.net_error));
            Log.i(TAG, "initLoginOut: -----"+throwable.toString());
        });
        dialog.dismiss();
        Utils.setToken(context);


    }
}
